/** TWI master interface.
 *
 * This file shall give a interface to the TWI bus. The user will be the master
 * of the bus.
*/
//##############################################################################




/** Use of the twi asf functions. */
#include "twi.h"
//------------------------------------------------------------------------------




/** Used to provide gpio pin mappings */
#include "gpio.h"
//##############################################################################




/** Init the AVR32_TWI in master mode.
 *
 * This function inits the AVR32_TWI in master mode. This includes setting the
 * appropriate GPIO pins and calling the asf twi master initialisation.
 *
 * \parm fcpu Frequency of the CPU.
 * \parm speed Speed of the TWI bus.
 * \parm master_address The address of the master.
 *
 * \return 1 on success, 0 otherwise
*/
signed char LR_twimaster_init( unsigned long fcpu,
    unsigned long speed,
    char master_address )
{
  static const gpio_map_t mgpio = {
    {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION},
    {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION}
  };
  static twi_options_t opt;

  gpio_enable_module( mgpio, sizeof( mgpio ) / sizeof( mgpio[0] ) );
  opt.pba_hz = fcpu;
  opt.speed = speed;
  opt.chip = master_address;
  return (twi_master_init( &AVR32_TWI, &opt ) == TWI_SUCCESS) ? (1) : (0);
}
//------------------------------------------------------------------------------




/** Send a package as master to a slave.
 *
 * This function shall be used to send a twi package to a slave. This
 * function is blocking!
 *
 * \parm slave_address Address of the slave.
 * \parm command Command / Address to send.
 * \parm command_len Length of the command (1 - 3).
 * \parm buffer Pointer to the data which will be send.
 * \parm bytes Number of bytes to send from the data location.
 *
 * \return 1 on success, 0 otherwise.
*/
signed char LR_twimaster_send( char slave_address, unsigned int command,
    unsigned char command_len, void * buffer, unsigned int bytes )
{
  // The package with the information about what is to be send to whom.
  twi_package_t send;

  // Initializing the package
  send.chip = slave_address;
  /*send.addr[0] = command >> 16;
  send.addr[1] = command >> 8;
  send.addr[2] = command;*/
  send.addr = command;
  send.addr_length = command_len;
  send.buffer = buffer;
  send.length = bytes;
  // Sending, should be blocking!
  return (twi_master_write( &AVR32_TWI, &send ) == TWI_SUCCESS) ? (1) : (0);
}
//------------------------------------------------------------------------------




/** Read a package as master from a slave.
 *
 * This function shall be used to read a twi package from a slave. This
 * function is blocking!
 *
 * \parm slave_address Address of the slave.
 * \parm command Command / Address to read from.
 * \parm command_len Length of the command.
 * \parm buffer Pointer to the place where the data should be stored.
 * \parm bytes Number of bytes to store in the buffer.
 *
 * \return 1 on success, 0 otherwise.
*/
signed char LR_twimaster_read( char slave_address, unsigned int command,
    unsigned char command_len, void * buffer, unsigned int bytes )
{
  // The package with the information about what is to be recieved from whom.
  twi_package_t rec;

  // Init the package
  rec.chip = slave_address;
  /*rec.addr[0] = command >> 16;
  rec.addr[1] = command >> 8;
  rec.addr[2] = command;*/
  rec.addr = command;
  rec.addr_length = command_len;
  rec.buffer = buffer;
  rec.length = bytes;
  // Recieve, should be blocking!
  return (twi_master_read( &AVR32_TWI, &rec ) == TWI_SUCCESS) ? (1) : (0);
}
//##############################################################################




